package com.hzy.cms.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.hzy.cms.domain.vo.AreaInfoVo;
import com.hzy.cms.domain.bo.AreaInfoBo;
import com.hzy.cms.service.IAreaInfoService;

/**
 * 中国地区
 *
 * @author zengxin
 * @date 2023-10-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cms/area")
public class AreaInfoController extends BaseController {

    private final IAreaInfoService iAreaInfoService;

    /**
     * 查询中国地区列表
     */
    @SaCheckPermission("cms:area:list")
    @GetMapping("/list")
    public TableDataInfo<AreaInfoVo> list(AreaInfoBo bo, PageQuery pageQuery) {
        return iAreaInfoService.queryPageList(bo, pageQuery);
    }


    /**
     * 导出中国地区列表
     */
    @SaCheckPermission("cms:area:export")
    @Log(title = "中国地区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AreaInfoBo bo, HttpServletResponse response) {
        List<AreaInfoVo> list = iAreaInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "中国地区", AreaInfoVo.class, response);
    }

    /**
     * 获取中国地区详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cms:area:query")
    @GetMapping("/{id}")
    public R<AreaInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iAreaInfoService.queryById(id));
    }

    /**
     * 新增中国地区
     */
    @SaCheckPermission("cms:area:add")
    @Log(title = "中国地区", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AreaInfoBo bo) {
        return toAjax(iAreaInfoService.insertByBo(bo));
    }

    /**
     * 修改中国地区
     */
    @SaCheckPermission("cms:area:edit")
    @Log(title = "中国地区", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AreaInfoBo bo) {
        return toAjax(iAreaInfoService.updateByBo(bo));
    }

    /**
     * 删除中国地区
     *
     * @param ids 主键串
     */
    @SaCheckPermission("cms:area:remove")
    @Log(title = "中国地区", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAreaInfoService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 导出中国地区列表
     */
    @SaCheckPermission("cms:area:export")
    @Log(title = "中国地区", businessType = BusinessType.EXPORT)
    @PostMapping("/export/region.json")
    public R<List<AreaInfoVo>> exportJson(AreaInfoBo bo, HttpServletResponse response) {
        List<AreaInfoVo> list = iAreaInfoService.queryList(bo);
        return R.ok(list);
    }
}
